@extends('base')

@section('content')
<div class="layui-card">
    <div class="layui-card-body">
        <table id="list-table" lay-filter="list-table"></table>
    </div>
</div>

<script type="text/html" id="head-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll"><i class="layui-icon layui-icon-spread-left"></i>展开</button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll"><i class="layui-icon layui-icon-shrink-right"></i>折叠</button>
</script>

<script type="text/html" id="line-bar">
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="append"><i class="layui-icon layui-icon-addition"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="menu-type">
    @{{#if (d.menu_type == '1') { }}
    <span>功能</span>
    @{{# }else if(d.menu_type == '2'){ }}
    <span>菜单</span>
    @{{# } }}
</script>

<script type="text/html" id="menu-status">
    <input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="启用|停用" lay-filter="status-enable"  @{{ d.menu_status== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="icon">
    <i class="layui-icon @{{d.menu_icon}}"></i>
</script>
@endsection

@section('js')
<script>
    layui.use(['table','form','jquery','treetable'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;

        window.render = function(){
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'pid',
                skin:'line',
                treeDefaultClose: true,
                toolbar:'#head-toolbar',
                elem: '#list-table',
                url: '/menu/list',
                page: false,
                cols: [
                    [
                        {title: '排序', field: 'menu_sort', width: 80,},
                        {title: '名称', field: 'menu_name', minWidth: 200,},
                        {title: '类型', field: 'menu_type', templet:'#menu-type'},
                        {title: '地址', field: 'menu_url', minWidth: 200,},
                        {title: '图标', field: 'menu_icon', templet:'#icon'},
                        {title: '状态', field: 'menu_status', templet:'#menu-status'},
                        {title: '操作', templet: '#line-bar', width: 150, align: 'center'}
                    ]
                ],
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
        }

        render();

        table.on('tool(list-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'append') {
                window.append(obj);
            }
        })

        table.on('toolbar(list-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            }  else if(obj.event === 'expandAll'){
                treetable.expandAll("#list-table");
            } else if(obj.event === 'foldAll'){
                treetable.foldAll("#list-table");
            }
        });

        window.add = function(){
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/menu/add'
            });
        }

        window.append = function(obj) {
            layer.open({
                type: 2,
                title: '新增子类',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/menu/add/' + obj.data['id']
            });
        }

        window.edit = function(obj){
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '500px'],
                content: '/menu/edit/' + obj.data['id']
            });
        }

        form.on('switch(status-enable)', function(obj) {
            $.ajax({
                url: "/menu/status/" + this.value,
                dataType: 'json',
                type: 'get',
                // async: false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(result) {
                    if (result.status) {
                        layer.msg(result.msg, {icon: 1, time: 1000})
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                    // window.render()
                }
            })
            return false
        });

        window.remove = function(obj) {
            layer.confirm('确定要删除吗？', {icon: 3, title: '提示'}, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/menu/delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(result) {
                        layer.close(loading);
                        if (result.status) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            window.render()
            // treetable.reload()
        }
    })
</script>
@endsection
